package kburndown.client.service
{
	import flash.utils.setTimeout;
	
	import kburndown.client.app.utils.DateHelper;
	import kburndown.client.service.data.ProjectInfo;
	import kburndown.client.service.data.TaskInfo;
	import kburndown.client.service.data.UnitInfo;
	
	import mx.rpc.events.ResultEvent;

	public class OfflineService implements IService
	{
		public function OfflineService()
		{
		}
		
		private function getNewId(): int
		{
			var units: Vector.<UnitInfo> = ProjectInfo.instance.units;
			var maxId: int = -1;
			for(var unitIndex: int = 0; unitIndex < units.length; unitIndex++)
			{
				var unit: UnitInfo = units[unitIndex];
				for(var infoIndex: int = 0; infoIndex < unit.tasks.length; infoIndex ++)
				{
					var task: TaskInfo = unit.tasks[infoIndex];
					if(task.id > maxId)
						maxId = task.id;
				}
			}
			return maxId + 1;
		}
		
		public function getInitData(callback: Function = null): void
		{
			ProjectInfo.instance.importData(callback);
		}
		
		public function addTask(name: String, content: String, icon: String, ownerId: int, duration: Number, callback: Function = null): void
		{
			showToolTip(KBurndownService.Method_AddTask);
			var task: TaskInfo = new TaskInfo();
			task.name = name;
			task.content = content;
			task.icon = icon;
			task.owner = UnitInfo.getUnitById(ownerId);
			task.duration = duration;
			task.id = getNewId();
			ProjectInfo.instance.addTaskByData(task);
			ProjectInfo.instance.changeTodayResult(task.duration);
			
			if (callback != null)
//				callback(ResultEvent.RESULT, task);
				setTimeout(callback, 0, ResultEvent.RESULT, task);
		}
		
		public function editTask(taskId: int, name: String, content: String, icon: String, duration: Number, callback: Function = null): void
		{
			showToolTip(KBurndownService.Method_EditTask);
			var taskInfo: TaskInfo = ProjectInfo.instance.getTaskById(taskId);
			var _oldDuration: Number = taskInfo.duration;
			taskInfo.name = name;
			taskInfo.content = content;
			taskInfo.icon = icon;
			taskInfo.duration = duration;
			var _newDuration: Number = taskInfo.duration;
			if (_oldDuration != _newDuration)
			{
				var changeValue: Number = _newDuration - _oldDuration;
				ProjectInfo.instance.changeTodayResult(changeValue);
			}
			
			if (callback != null)
//				callback(ResultEvent.RESULT);
				setTimeout(callback, 0, ResultEvent.RESULT);
		}
		
		public function stretchTaskDuration(task: TaskInfo, newDuration: int, callback: Function = null): void
		{
			editTask(task.id, task.name, task.content, task.icon, newDuration, callback);
		}
		
		public function moveTask(taskId: int, anchorTaskId: int, callback: Function = null): void
		{
			var task: TaskInfo = ProjectInfo.instance.getTaskById(taskId);
			var unit: UnitInfo = task.owner;
			unit.setMoveTask(taskId, anchorTaskId);
			if (callback != null)
//				callback(ResultEvent.RESULT);
				setTimeout(callback, 0, ResultEvent.RESULT);
		}
		
		public function finishTask(taskId: int, comment: String, callback: Function = null): void
		{
			showToolTip(KBurndownService.Method_FinishTask);
			var task: TaskInfo = ProjectInfo.instance.getTaskById(taskId);
			task.finishedDate = DateHelper.getToday();
			var unit: UnitInfo = task.owner;
			unit.setTaskFinished(task);
			
			ProjectInfo.instance.changeTodayResult(-task.duration);
//			callback(ResultEvent.RESULT, task);
			setTimeout(callback, 0, ResultEvent.RESULT, task);
		}
		
		public function deleteTask(taskId: int, callback: Function = null): void
		{
			showToolTip(KBurndownService.Method_DeleteTask);
			var task: TaskInfo = ProjectInfo.instance.getTaskById(taskId);
			//			task.finishedDate = DateHelper.getToday();
			task.deleted = true;
			var unit: UnitInfo = task.owner;
			unit.setTaskDeleted(task);
			ProjectInfo.instance.changeTodayResult(-task.duration);
//			callback(ResultEvent.RESULT, task);
			setTimeout(callback, 0, ResultEvent.RESULT, task);
		}
		
		public function moveUnit(unitId: int, posX: Number, posY: Number, callback: Function = null): void
		{
			showToolTip(KBurndownService.Method_MoveUnit);
			var unit: UnitInfo = UnitInfo.getUnitById(unitId);
			unit.posX = posX;
			unit.posY = posY;
			if (callback != null)
				setTimeout(callback,0, ResultEvent.RESULT);
		}
		
		public function restore(taskId: int, anchorTaskId: int, callback: Function): void
		{
			var task: TaskInfo = ProjectInfo.instance.getTaskById(taskId);
			ProjectInfo.instance.setTaskReOpen(task, anchorTaskId);
			if(callback != null)
			{
				callback(ResultEvent.RESULT, task);
			}
		}
		
		public function updataGraphData(date: Date, callback: Function):void
		{
			
		}
		
		private function showToolTip(method: String): void
		{
			ServiceToolTip.showTooltip(method);
			ServiceToolTip.hideToolTip();
		}
		
		
		
		//------------------------------------------------------------------------------------
		//	
		//		伪数据
		//
		//--------------------------------------------------------------------------------------
		private function _updateGraph(date: Date): void
		{
			
		}
		
	}
}